package team.jaman.autosell.controller;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import team.jaman.autosell.entity.Logger;
import team.jaman.autosell.entity.Manager;
import team.jaman.autosell.entity.Product;
import team.jaman.autosell.service.ProductService;
import team.jaman.autosell.tools.CommonData;
import team.jaman.autosell.tools.PageData;
/**
 * 商品控制器
 * @author Administrator
 *
 */
@Controller
@RequestMapping("product")
public class ProductController {
	
	@Autowired
	private ProductService productService;
	
	/**
	 * 列表
	 * @param page 当前页
	 * @param limit 每页记录数
	 * @param keyword 商品名称关键字
	 * @return
	 */
	@GetMapping(value="productList", produces="application/json;charset=utf-8")
	@ResponseBody
	public PageData<Product> getProductByLike(@RequestParam(value="page",defaultValue="1") int page, @RequestParam(value="limit",defaultValue="20") int limit, @RequestParam(value="keyword",defaultValue="") String keyword){
		return productService.selectByExample(page, limit, keyword);		
	}
	
	/**
	 * 修改
	 * @param product 商品
	 * @return
	 */
	@PutMapping(value="updateProduct",produces="application/json;charset=utf-8",consumes="application/json")
	@ResponseBody
	public CommonData updateProductById(@RequestBody Product product,Manager manager) {
		Logger logger = new Logger();
		logger.setStartTime(new Date());
		return productService.updateProductById(product,manager,logger);
	}
	
	/**
	 * 删除
	 * @param productId 商品编号
	 * @return
	 */
	@DeleteMapping(value="deleteProduct/{id}")
	@ResponseBody
	public CommonData deleteProductById(@PathVariable("id") int productId,Manager manager) {
		Logger logger = new Logger();
		logger.setStartTime(new Date());
		return productService.deleteProductById(productId,manager,logger);
	}
	
	/**
	 * 增加
	 * @param product
	 * @return
	 */
	@PostMapping("addProduct")
	@ResponseBody
	public CommonData addProduct(@RequestBody Product product,Manager manager) {
		Logger logger = new Logger();
		logger.setStartTime(new Date());
		return productService.addProduct(product,manager,logger);
	}
	
	@PostMapping("fileupload")
	@ResponseBody
	public CommonData fileUpload(@RequestPart("file") MultipartFile file) throws IOException {
		// 输入流
		InputStream is = file.getInputStream();
		// 上传路径
		String path = "F:\\中软\\images\\" + file.getOriginalFilename();
		System.out.println(path);
		// 输出流
		OutputStream os = new FileOutputStream(path);
		// 定义一个数组
		byte[] bytes = new byte[1024];
		
		int len = 0;
		while((len = is.read(bytes)) != -1) {
			os.write(bytes, 0, len);
		}
		os.close();
		is.close();
		// 通用类
		CommonData commonData = new CommonData();
		commonData.setCode(200);
		commonData.setMsg("上传成功");
		commonData.setSrc("/pages/images/" + file.getOriginalFilename());
		return commonData;
	}
	
}
